package cn.maihe.elg.operation.centers.follow.channel;

import cn.maihe.elg.operation.centers.follow.channel.dto.FollowBaseRespDTO;
import cn.maihe.elg.operation.centers.follow.channel.dto.GuarantorClaimSettleNoticeReqDTO;
import cn.maihe.elg.operation.centers.follow.channel.dto.GuarantorRevokeNoticeReqDTO;
import cn.maihe.elg.operation.centers.follow.channel.dto.GuarantorStatusNoticeReqDTO;
import cn.maihe.elg.operation.centers.follow.channel.service.FollowPayResultReceiveChannelService;
import cn.maihe.elg.operation.centers.follow.channel.service.GuarantorClaimSettleNoticeChannelService;
import cn.maihe.elg.operation.centers.follow.channel.service.GuarantorRevokeNoticeChannelService;
import cn.maihe.elg.operation.centers.follow.channel.service.GuarantorStatusNoticeChannelService;
import cn.maihe.elg.operation.centers.supports.CenterInterfaceService;
import cn.maihe.elg.operation.centers.supports.model.bo.GuaranteeClaimSettlementResultBo;
import cn.maihe.elg.operation.centers.supports.model.bo.GuaranteeRevokeResultBo;
import cn.maihe.elg.operation.centers.supports.model.bo.NoticePayResultBo;
import cn.maihe.elg.operation.centers.supports.model.bo.PushCenterGuaranteeBo;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.RefundOrderStatus;
import cn.maihe.elg.operation.repository.entity.GuaranteeDO;
import cn.maihe.elg.operation.repository.entity.RefundOrderInfoDO;
import cn.maihe.elg.operation.service.guarantee.GuaranteeService;
import cn.maihe.elg.operation.utils.ElgLogger;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description 随行平台接口服务实现
 * @Author Alan
 * @Date 2024/12/10
 */
@Service
@Slf4j
public abstract class BaseFollowCenterInterfaceService implements CenterInterfaceService {

    @Resource
    private GuarantorRevokeNoticeChannelService guarantorRevokeNoticeChannelService;
    @Resource
    private GuarantorClaimSettleNoticeChannelService guarantorClaimSettleNoticeChannelService;
    @Resource
    private GuarantorStatusNoticeChannelService guarantorStatusNoticeChannelService;
    @Resource
    private GuaranteeService guaranteeService;
    @Resource
    private FollowPayResultReceiveChannelService followPayResultReceiveChannelService;


    @Override
    public InnerBaseResp<String> pushGuarantee(PushCenterGuaranteeBo bo) {
        GuaranteeDO curGuaranteeDO = GuaranteeDO.of(bo.getGuaranteeVo());
        GuarantorStatusNoticeReqDTO noticeReqDTO = bo.getGuarantorStatusNoticeReqDTO();
        InnerBaseResp innerBaseResp = null;
        try {
            FollowBaseRespDTO respDTO = guarantorStatusNoticeChannelService.doRequest(noticeReqDTO);
            if (respDTO != null && respDTO.isSuccess()) {
                curGuaranteeDO.setGmtModified(new Date());
                curGuaranteeDO.setPushFlag(respDTO.isSuccess() ? 1 : 0);
                curGuaranteeDO.setPushRespMsg(respDTO.getMsg());
                guaranteeService.updateById(curGuaranteeDO);
                innerBaseResp = InnerBaseResp.success(curGuaranteeDO.getGuaranteeNo());
            } else {
                innerBaseResp = InnerBaseResp.error("异步推送电子保函响应异常");
                ElgLogger.info(log, log -> log.info("受理订单号[{}] >> 异步推送电子保函响应异常：{}", curGuaranteeDO.getAcceptOrderNo()));
//                noticeRetryInfoService.saveOrUpdateNoticeRetryInfo(NoticeTypeEnum.guarantee_apply_result,
//                        curGuaranteeDO.getCenterId(), curGuaranteeDO.getOrgId(),
//                        curGuaranteeDO.getAcceptOrderNo(), noticeReqDTO, null, respDTO
//                );
            }
            ElgLogger.info(log, log -> log.info("受理订单号[{}] >> 异步推送电子保函至交易中心响应报文：{}", curGuaranteeDO.getAcceptOrderNo(), JSON.toJSONString(respDTO)));
        } catch (Exception e) {
            innerBaseResp = InnerBaseResp.error(e.getMessage());
            ElgLogger.info(log, log -> log.info("受理订单号[{}] >> 异步推送电子保函异常：{}", curGuaranteeDO.getAcceptOrderNo(), e.getMessage(), e));
//            noticeRetryInfoService.saveOrUpdateNoticeRetryInfo(NoticeTypeEnum.guarantee_apply_result,
//                    curGuaranteeDO.getCenterId(), curGuaranteeDO.getOrgId(),
//                    curGuaranteeDO.getAcceptOrderNo(), noticeReqDTO, null, innerBaseResp
//            );
        }
        return innerBaseResp;
    }

    /**
     * 保函撤销|退保结果通知
     *
     * @param bo
     * @return
     */
    @Override
    public InnerBaseResp noticeGuaranteeRevokeResult(GuaranteeRevokeResultBo bo) {
        GuarantorRevokeNoticeReqDTO reqDTO = new GuarantorRevokeNoticeReqDTO();
        reqDTO.setPolicyNo(bo.getGuaranteeNo());
        InnerBaseResp<String> innerBaseResp = null;

        if (bo.getAuditPass()) {
            reqDTO.setStatus("7");
            reqDTO.setMsg(bo.getAuditRemark());
        } else {
            reqDTO.setStatus("0");
            reqDTO.setMsg(bo.getAuditRemark());
        }

        try {
            FollowBaseRespDTO respDTO = guarantorRevokeNoticeChannelService.doRequest(reqDTO);
            Assert.notNull(respDTO, "响应报文不能为空");
            if (!"200".equals(respDTO.getCode())) {
                innerBaseResp = InnerBaseResp.error(respDTO.getMsg());
            } else {
                innerBaseResp = InnerBaseResp.success(reqDTO.getPolicyNo());
                log.info("保函编号[{}] >> 撤销结果回调通知成功", bo.getGuaranteeNo());
            }
        } catch (Exception e) {
            log.error("保函撤销结果通知 >> [{}] 异常:{}", getCenterNoEnum().getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("通知异常:" + e.getMessage());
        }
        return innerBaseResp;
    }

    /**
     * 保函理赔申请结果通知
     *
     * @param bo
     * @return
     */
    @Override
    public InnerBaseResp noticeGuaranteeClaimSettlementResult(GuaranteeClaimSettlementResultBo bo) {
        RefundOrderInfoDO refundOrderInfo = bo.getRefundOrderInfo();
        GuarantorClaimSettleNoticeReqDTO reqDTO = new GuarantorClaimSettleNoticeReqDTO();
        reqDTO.setPolicyNo(refundOrderInfo.getGuaranteeNo());
        reqDTO.setStatus("2");
        reqDTO.setMsg("理赔完成");
        InnerBaseResp<String> innerBaseResp = null;
        if (refundOrderInfo.getStatus() == RefundOrderStatus.REFUNDED.getStatus()) {
            reqDTO.setStatus("2");
            reqDTO.setMsg(refundOrderInfo.getRemark());
        } else if (refundOrderInfo.getStatus() == RefundOrderStatus.REFUSED.getStatus()) {
            reqDTO.setStatus("0");
            reqDTO.setMsg(refundOrderInfo.getRemark());
        } else {
            return InnerBaseResp.success(refundOrderInfo.getGuaranteeNo());
        }

        try {
            FollowBaseRespDTO respDTO = guarantorClaimSettleNoticeChannelService.doRequest(reqDTO);
            Assert.notNull(respDTO, "响应报文不能为空");
            if (!"200".equals(respDTO.getCode())) {
                innerBaseResp = InnerBaseResp.error(respDTO.getMsg());
            } else {
                innerBaseResp = InnerBaseResp.success(reqDTO.getPolicyNo());
                log.info("保函编号[{}] >> 理赔结果回调通知成功", refundOrderInfo.getGuaranteeNo());
            }
        } catch (Exception e) {
            log.error("理赔结果通知 >> [{}] 异常:{}", getCenterNoEnum().getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("通知异常:" + e.getMessage());
        }
        return innerBaseResp;
    }

    /**
     * 随行无需进行支付结果推送，重写方法 默认返回成功
     *
     * @param bo
     * @return
     */
    @Override
    public InnerBaseResp noticePayResult(NoticePayResultBo bo) {
        return followPayResultReceiveChannelService.noticePayResult(bo);
    }

}
